package orz.mikeneck.server.locrec.controller;

import orz.mikeneck.server.locrec.exception.ApiNotSupportedException;


public enum ApiHandlerMatcher implements ApiHandlerUtil {
	NEW {
		private String modeValue = "new";

		@Override
		public ApiHandler getHandler() {
			return new ApiPostNewRecord();
		}

		@Override
		protected boolean isModeCollect(String mode) {
			return modeValue.equals(mode);
		}
	},
	DELETE {
		private String modeValue = "delete";

		@Override
		public ApiHandler getHandler() {
			return new ApiPostDeleteRecord();
		}

		@Override
		protected boolean isModeCollect(String mode) {
			return modeValue.equals(mode);
		}
	},
	GET {
		private String modeValue = ApiHandler.GetParams.MODE_GET;

		@Override
		public ApiHandler getHandler() {
			return new ApiGetRecord();
		}

		@Override
		protected boolean isModeCollect(String mode) {
			return modeValue.equals(mode);
		}
	};

	@Override
	public abstract ApiHandler getHandler();

	protected abstract boolean isModeCollect(String mode);

	public static ApiHandler getHandler(String mode) throws ApiNotSupportedException{
		for(ApiHandlerMatcher match : ApiHandlerMatcher.values()){
			if(match.isModeCollect(mode)){
				return match.getHandler();
			}
		}
		throw new ApiNotSupportedException();
	}
}
